package org.edu.modules.research.rproject.service.impl;

import org.edu.common.base.BasePageDto;
import org.edu.common.base.BaseServiceImpl;
import org.edu.common.utils.ConvertUtils;
import org.edu.common.base.BaseServiceImpl;
import org.edu.modules.research.rproject.mapper.VeJkyProjectUserMapper;
import org.edu.modules.research.rproject.entity.VeJkyProjectUser;
import org.edu.modules.research.rproject.dto.VeJkyProjectUserDto;
import org.edu.modules.research.rproject.service.VeJkyProjectUserService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 实现类
 */
@Service
public class VeJkyProjectUserServiceImpl extends BaseServiceImpl<VeJkyProjectUser, VeJkyProjectUserMapper> implements VeJkyProjectUserService {

    public IPage<VeJkyProjectUserDto> pageDto(BasePageDto page, @Param(Constants.WRAPPER) Wrapper queryWrapper){
        return mapper.pageDto(page,queryWrapper);
    }
    /**
    * 查询实体类
    */
    public VeJkyProjectUserDto findDtoById(String id) {
        VeJkyProjectUser entity = super.findById(id);
        VeJkyProjectUserDto dto = ConvertUtils.convertToDto(entity, VeJkyProjectUserDto.class);
        return dto;
    }

    /**
    * 新增或者修改
    */
    public void insertOrUpdate(VeJkyProjectUserDto dto,Boolean isAdd){
        if(isAdd){
            List<VeJkyProjectUser> users = new ArrayList<>();
            for (Integer userId:dto.getUserIds()) {
                VeJkyProjectUser user = new VeJkyProjectUser();
                user.setJkyUserId(userId);
                user.setTextId(dto.getTextId());
                user.setProjectId(dto.getProjectId());
                user.setStatus(dto.getStatus());
                user.setJoinTime(new Date());
                users.add(user);
            }
            super.insert(users);
        }else{
            super.update(dto);
        }
    }

}
